package com.diandian.backend.kuaishou.life.exchange.service;

import com.alibaba.fastjson.JSON;
import com.diandian.backend.kuaishou.life.exchange.client.DianDianStoreClient;
import com.diandian.backend.kuaishou.life.exchange.client.dto.StoreKuaishouLifeStoreInfoDetailByCodeParam;
import com.diandian.backend.kuaishou.life.exchange.client.dto.StoreKuaishouLifeStoreInfoDetailResp;
import com.diandian.backend.kuaishou.life.exchange.resp.KuaishouLifeCertificatePrepareWithOrderDetailResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.kuaishou.life.boot.starter.dto.CertificatePrepareRespDTO;
import com.diandian.kuaishou.life.boot.starter.dto.CertificateVerifyRespDTO;
import com.diandian.kuaishou.life.boot.starter.dto.OrderDetailQueryRespDTO;
import com.diandian.kuaishou.life.boot.starter.param.CertificatePrepareParam;
import com.diandian.kuaishou.life.boot.starter.param.CertificateVerifyParam;
import com.diandian.kuaishou.life.boot.starter.service.KuaiShouCertificateBasicService;
import com.diandian.kuaishou.life.boot.starter.service.KuaiShouLifeOrderBasicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusinessCertificateVerifyService {

    private static final Logger logger = LoggerFactory.getLogger(BusinessCertificateVerifyService.class);

    @Autowired
    private DianDianStoreClient dianDianStoreClient;

    @Autowired
    private KuaiShouCertificateBasicService certificateBasicService;

    @Autowired
    private KuaiShouLifeOrderBasicService orderBasicService;

    /**
     * 验券准备，并返回订单详情
     * @param accessToken
     * @param param
     * @return
     */
    public KuaishouLifeCertificatePrepareWithOrderDetailResp prepareWithOrderDetail(String accessToken, CertificatePrepareParam param){

        // 验券准备信息
        CertificatePrepareRespDTO certificatePrepareRespDTO = certificateBasicService.prepare(accessToken, param);
        String orderId = certificatePrepareRespDTO.getData().getOrder_id();

        // 订单信息
        OrderDetailQueryRespDTO orderDetail = orderBasicService.getOrderDetail(accessToken, orderId);

        return new KuaishouLifeCertificatePrepareWithOrderDetailResp(certificatePrepareRespDTO.getData(), orderDetail.getData());
    }

    /**
     * 使用线下门店编码核销
     * @param param
     * @param accessToken
     * @return
     */
    public CertificateVerifyRespDTO verifyWithOfflineStoreCode(String belong, String accessToken, CertificateVerifyParam param){

        logger.info("执行使用线下门店编码核销，参数（belong|accessToken:param）:{} | {} | {}", belong, accessToken, JSON.toJSONString(param));

        // 门店编码转换：线下转线上
        String offlineStoreCode = param.getPoi_id();
        String onlineStoreCode = offlineStoreCodeToOnlineStoreCode(belong, offlineStoreCode);
        logger.info("使用线下门店编码{}兑换获得线上门店编码：{}", offlineStoreCode, onlineStoreCode);

        param.setPoi_id(onlineStoreCode);

        // 调用
        return certificateBasicService.verify(accessToken, param);

    }

    private String offlineStoreCodeToOnlineStoreCode(String belong, String offlineStoreCode){

        StoreKuaishouLifeStoreInfoDetailByCodeParam param = new StoreKuaishouLifeStoreInfoDetailByCodeParam();
        param.setOfflineCode(offlineStoreCode);
        param.setBelong(belong);
        BaseResult baseResult = dianDianStoreClient.getDetailByCode(param);
        if(baseResult == null || baseResult.getCode() != 0 || baseResult.getData() == null){
            throw new DefaultException("未找到此门店");
        }

        StoreKuaishouLifeStoreInfoDetailResp detailResp = JSON.parseObject(JSON.toJSONString(baseResult.getData()), StoreKuaishouLifeStoreInfoDetailResp.class);
        if(detailResp == null || detailResp.getOnlineCode() == null || "".equals(detailResp.getOnlineCode())){
            throw new DefaultException("未找到此门店");
        }

        return detailResp.getOnlineCode();
    }
}
